/*
 * Copyright (C) 2023 KylinSoft Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
**/
#include "languagesetting.h"
#include <QApplication>
#include "definetypes.h"
#include <QDebug>

LanguageSetting* LanguageSetting::m_singleInstance = nullptr;

LanguageSetting::LanguageSetting(QObject *parent)
    : QObject(parent)
{

}

LanguageSetting::~LanguageSetting()
{

}

LanguageSetting* LanguageSetting::instance(QObject *parent)
{
    if(m_singleInstance == nullptr)
        m_singleInstance = new LanguageSetting(parent);
    return m_singleInstance;
}

void LanguageSetting::onLanguageChanged(QString languageCode)
{
    if(languageCode == "")
        return ;

    if(m_strLanguageCode == languageCode) {
        return;
    }

    qDebug()<<"onLanguageChanged "<<languageCode;
    Q_EMIT languageChanged(false);
    if (m_translator) {
        qApp->removeTranslator(m_translator);
        delete m_translator;
        m_translator = nullptr;
    }
    m_translator = new QTranslator(this);
    QString qmFile;

    if (languageCode.startsWith("zh_CN")) { /*这里写的有问题，忘记之前为什么写了，会导致繁体也会加载zh_CN*/
        qmFile = QString(WORKING_DIRECTORY"/i18n_qm/%1.qm").arg("zh_CN");
        setenv("LANGUAGE","zh_CN",1);
        //setenv("LANG","zh_CN",1);
        setlocale(LC_ALL,"zh_CN.utf8");
        QLocale lang("zh_CN");
        /*这里设置一个QLocale的默认值，用来影响插件的语言。插件加载翻译文件时，不使用QLocale::system().name()来获取语言，
         * 应该使用QLocale local; local.name() 来获取语言,这样可以识别到登录界面设置的的语言环境*/
        QLocale::setDefault(lang);
    } else {
        qmFile = QString(WORKING_DIRECTORY"/i18n_qm/%1.qm").arg(languageCode);
        setenv("LANGUAGE",languageCode.toLatin1().data(),1);
        //setenv("LANG",languageCode.toLatin1().data(),1);
        setlocale(LC_ALL,"");
        QLocale lang(languageCode);
        /*这里设置一个QLocale的默认值，用来影响插件的语言。插件加载翻译文件时，不使用QLocale::system().name()来获取语言，
             * 应该使用QLocale local; local.name() 来获取语言,这样可以识别到登录界面设置的的语言环境*/
        QLocale::setDefault(languageCode);
    }

    m_translator->load(qmFile);
    qApp->installTranslator(m_translator);
    qDebug() << "load translation file " << qmFile;

    m_strLanguageCode = languageCode;
    Q_EMIT languageChanged(true);
}
